package com.ucs.ui;

import java.util.List;

import com.ucs.domain.User;
import com.ucs.util.UCSData;

//import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SigninActivity extends SimpleActivity implements SimpleActivity.ResultCallbackIF{
    private static final String TAG = "ucs";
//	private Button signin;
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		UCSData.getInstance().setContext(this);
    	
		final List<User> users = UCSData.getInstance().getUsers();
        Log.v(TAG, "SigninActivity:users.size()="+users.size());
		
		setContentView(R.layout.signin);
		final	SigninActivity thisSigninActivity = this;
        Button signin_btn = (Button) findViewById(R.id.btn_sign_in);
        signin_btn.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  EditText etxt_pass = (EditText) findViewById(R.id.txt_password);
                  String password = etxt_pass.getText().toString();
                  for (User user : users) {
                      if (password.equals(user.getPwd())){
                    	  UCSData.getInstance().setLoggedInUser(user);
                    	  launchSubActivity(MainActivity.class, thisSigninActivity, null);
                      }					
                  }
              }
        });
        
        Button setup_btn = (Button) findViewById(R.id.btn_setup);
        setup_btn.setOnClickListener(new View.OnClickListener() {
        	@Override
        	public void onClick(View arg0) {
    			launchSubActivity(DownloadActivity.class, thisSigninActivity, null);        		
        	}
        });
		if (users.size() == 0){
			launchSubActivity(DownloadActivity.class, this, null);
			return;
		}
    }

	@Override
	public void resultCancel(Intent intent) {
		Log.d("ucs", "+SigninActivity:resultCancel");
		Log.d("ucs", "-SigninActivity:resultcancel");				
	}

	@Override
	public void resultOk(Intent intent) {
		Log.d("ucs", "+SigninActivity:resultOk");
		Log.d("ucs", "-SigninActivity:resultOk");		
	}
}